<template>
  <div class="equipmentBrand flowCloumn h100">
    <div class="h100">
      <list-table-new ref="listTable" v-bind="tableSetting"></list-table-new>
    </div>
    <dialogPublic
      :title="title"
      :show.sync="show"
      v-if="show"
      @doSubmit="doSubmit"
      @doClose="() => (show = false)"
    >
      <editForm
        :formConfig="formConfig"
        ref="editForm"
        :formData="editFormData"
      ></editForm>
    </dialogPublic>
    <dialogPublic
      :title="'导入文件'"
      :show.sync="showFile"
      width="40%"
      :noFootBtn="true"
    >
      <div style="text-align: left; height: 200px" v-if="showFile">
        <el-upload
          class="upload-demo"
          :action="uploadUrl"
          :on-change="handleChange"
          :multiple="false"
          :auto-upload="false"
          accept=".xls,.xlsx"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <el-button
            style="margin-left: 10px"
            size="small"
            type="success"
            @click="submitUpload"
            >上传到服务器</el-button
          >
          <div class="el-upload__tip" slot="tip">
            只能上传excle文件，且不超过500kb
          </div>
        </el-upload>
        <el-button v-if="path != ''" type="text" @click="downLoadResult"
          >下载错误备注文件</el-button
        >
      </div>
    </dialogPublic>
  </div>
</template>

<script>
import {
  brandStockList,
  brandStockAdd,
  brandStockEdit,
  brandStockDelete,
  importBrandStock,
} from "@/api/standardManagement/equipmentBrand.js";
export default {
  data() {
    return {
      show: false,
      showFile: false,
      title: "",
      path: "",
      searchData: {},
      uploadUrl: `${process.env.VUE_APP_BASEURL}/eam-service-standard/brandStock/import`,
      tableSetting: {
        showAllSearch: true,
        pageConfig: {
          pageCode: "equipmentBrand",
          userId: this.$store.getters.userInfo.id,
        },
        showIndex: true,
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          searchArr: [
            {
              type: "input",
              label: "关键字",
              model: "keyword",
              placeholder: "输入品牌简称关键字",
            },
          ],
          queryData: {
            page: 1,
            pageSize: 15,
          },
        },
        tableMethods: {
          rowClick: (row) => {},
        },
        actionList: [
          {
            name: "新增",
            icon: "el-icon-plus",
            disabled: !this.$store.getters.isBtnAuth("add"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: () => {
              this.show = true;
              this.title = "新增";
              this.editFormData = {};
            },
          },
          {
            name: "导入",
            icon: "icon-daoru",
            disabled: !this.$store.getters.isBtnAuth("import"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.showFile = true;
              //this.uploadFile();
            },
          },
          {
            name: "导出",
            icon: "icon-daochu",
            disabled: !this.$store.getters.isBtnAuth("export"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.export();
            },
          },
          {
            name: "下载模板",
            icon: "icon-xiazai",
            disabled: !this.$store.getters.isBtnAuth("download"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.download();
            },
          },
        ],
        column: [
          {
            label: "品牌简称",
            prop: "name",
            sortable: "custom",
          },
          {
            label: "品牌全称",
            prop: "factoryName",
            sortable: "custom",
          },
          {
            label: "使用频率",
            prop: "num",
            sortable: "custom",
          },
          {
            label: "创建时间",
            prop: "createTime",
            sortable: "custom",
          },
          {
            label: "创建人",
            prop: "createByName",
            sortable: "custom",
          },
          {
            label: "操作",
            type: "button",
            buttonArr: [
              {
                text: "编辑",
                isShow: () => {
                  return true;
                },
                disabled: !this.$store.getters.isBtnAuth("edit"), //控制按钮的禁用
                content: "未授权功能", //禁用提示内容
                methods: (row, index) => {
                  this.editFormData = row;
                  this.title = "编辑";
                  this.show = true;
                },
              },
              {
                text: "删除",
                isShow: () => {
                  return true;
                },
                disabled: !this.$store.getters.isBtnAuth("delete"), //控制按钮的禁用
                content: "未授权功能", //禁用提示内容
                methods: (row, index) => {
                  this.delete(row);
                },
              },
            ],
          },
        ],
        tableData: [],
        paginationObj: {
          pageSizes: [15, 30, 50, 100],
        },
        totalNumber: 0,
      },
      formConfig: {
        labelWidth: "100px",
        itemArr: [
          {
            label: "品牌简称",
            prop: "name",
            type: "text",
            span: 24,
            placeholder: "请输入",
            required: true,
          },
          {
            label: "品牌全称",
            prop: "factoryName",
            type: "text",
            span: 24,
            placeholder: "请输入",
            required: true,
          },
        ],
      },
      //弹出框表单数据
      editFormData: {},
      factoryName: "",
    };
  },
  created() {},
  mounted() {
    this.$refs.listTable.getTableConfig();
    this.$refs.listTable.searchData();
  },
  methods: {
    // 获取表格数据
    getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      this.searchData = params;
      if (sortStr.length > 0) {
        queryData.orders = sortStr;
      }
      brandStockList(queryData).then((res) => {
        this.$set(this.tableSetting, "tableData", res.data.records);
        this.$set(this.tableSetting, "totalNumber", res.data.total);
      });
    },
    //新增或修改提交
    async doSubmit() {
      try {
        let param = await this.$refs.editForm.getFormData();
        if (this.title == "新增") {
          let res = await brandStockAdd(param);
          this.$message.success("新增成功");
          this.show = false;
          this.$refs.listTable.searchData();
        } else {
          let res = await brandStockEdit(param);
          this.$message.success("修改成功");
          this.show = false;
          this.$refs.listTable.searchData();
        }
      } catch (error) {}
    },
    delete(row) {
      this.$confirm("确定要删除这条数据吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let params = {
            id: row.id,
          };
          brandStockDelete(params).then(
            (res) => {
              if (res.code === 200) {
                this.$message.success(res.description);
                this.$refs.listTable.searchData();
              }
            },
            (error) => {
              this.$message.error(error);
            }
          );
        })
        .catch(() => {});
    },
    //导出
    export() {
      let paramStr = "";
      let params = this.searchData;
      for (const key in params) {
        if (Object.hasOwnProperty.call(params, key)) {
          if (params[key] && params[key] != "") {
            paramStr += `${key}=${params[key]}&`;
          }
        }
      }
      paramStr = paramStr.substring(0, paramStr.length - 1);
      let path = `eam-service-standard/brandStock/export?${paramStr}`;
      let fileName = `设备品牌库`;
      this.$utils.downloadFile(null, fileName, path);
    },
    //文件上传之前
    handleChange(file) {
      let formData = new FormData();
      formData.append("file", file.raw);
      this.uploadFile = formData;
    },
    submitUpload() {
      importBrandStock(this.uploadFile).then((res) => {
        if (typeof res.data == "string") {
          this.path = `eam-service-standard/${res.data}`;
          this.$message.error("导入失败，请下载批复文件查看");
        } else {
          this.$message.success("导入成功");
          this.showFile = false;
          this.updateData();
        }
      });
    },
    //点击下载批复文件
    downLoadResult() {
      this.$utils.downloadFile(null, "设备品牌库批复明细", this.path);
    },
    download() {
      let path = "eam-service-standard/brandStock/template";
      let fileName = "设备品牌库导入模板";
      this.$utils.downloadFile(null, fileName, path);
    },
  },
};
</script>

<style lang="less" scoped>
.equipmentBrand {
  padding: 20px;
}
</style>